/**
 * http://code.google.com/p/gwt-spot
 * 
 * GWT-Spot -- Copyright 2008-2009
 *          -- by Dunkel, Ricardo
 *  
 */
package de.ricardodunkel.gwtspot.client.api.common;

import java.util.ArrayList;
import java.util.List;

/**
 * Utility class for easier string handling.
 * 
 * @author Dunkel, Ricardo
 * 
 * @version 0.11
 * 
 */
public class StringUtility {

	/**
	 * Just invert {@link #isEmpty(String)}.
	 * 
	 * @param text  A text to check for non-<em>emptiness</em>.
	 * 
	 * @return  <strong>True</strong> if <em>text</em> is not empty,
	 * 			<strong>false</strong> otherwise.
	 */
	public static boolean isNotEmpty(String text) {
		return !isEmpty(text);
	}
	
	/**
	 * Does check a text for existence of content.
	 * 
	 * @param text  A text to check for <em>emptiness</em>.
	 * 
	 * @return  <strong>True</strong> if <em>text</em> is empty or null,
	 * 			<strong>false</strong> otherwise.
	 */
	public static boolean isEmpty(String text) {
		if( null == text || text.length() == 0 ) {
			return true;
		} 
		return false;
	}
	
	/**
	 * Just invert {@link #isBlank(String)}.
	 * 
	 * @param text  A text to check for non-<em>blankness</em>.
	 * 
	 * @return  <strong>True</strong> if <em>text</em> is not blank,
	 * 			<strong>false</strong> otherwise.
	 */
	public static boolean isNotBlank(String text) {
		return !isBlank(text);
	}
	
	/**
	 * Does check a text for existence of content.
	 * 
	 * <p>
	 * 	In contrast to {@link #isEmpty(String)}, the parameter
	 *  will be <em>trim</em>med as well.
	 * </p>
	 * 
	 * @param text  A text to check for <em>blankness</em>.
	 * 
	 * @return  <strong>True</strong> if <em>text</em> is blank or null,
	 * 			<strong>false</strong> otherwise.
	 */
	public static boolean isBlank(String text) {
		if( null == text || text.trim().length() == 0 ) {
			return true;
		} 
		return false;
	}
	
	/**
	 * Does split a given string into a {@link List} of strings
	 * which were separated by given <em>token</em>.
	 * 
	 * <p>
	 * 	May act like <em>StringTokenizer</em> subset function.
	 * </p>
	 * 
	 * @param text  A text to split by <em>token</em>s.
	 * @param token  A token used as separator.
	 * 
	 * @return  A list of token, or an empty one (<em>not null</em>)
	 * 			if no token could be found.
	 */
	public static List<String> splitToken(String text, String token) {
		int start = 0;
		List<String> res = new ArrayList<String>();
		String tmp = null;
		while( text.indexOf(token, start) > 0 ) {
			tmp = text.substring(start, text.indexOf(token, start) );
			if( null != tmp ) {
				res.add( tmp.trim() );
			}
			start = text.indexOf(token, start)+token.length();
		}
		tmp = text.substring(start, text.length() );
		if( isNotBlank( tmp ) ) {
			res.add( tmp.trim() );
		}
		return res;
	}
	
}
